﻿//This filter is tested and seems to work correctly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    public enum DIRECTION_MODE
    {
        VERTICAL,
        HORIZONTAL,
        BOTH
    }

    //TODO: vertical / horizontal / mixed enum? momentarily only horizontal jittering is possible.
    /// <summary>
    /// 
    /// </summary>
    public class Jitter : BaseImageFilter
    {
        protected int m_nOffset = 2;

        /// <summary>
        /// default constructor
        /// </summary>
        public Jitter()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nOffset"></param>
        public Jitter(int _nOffset)
        {
            m_nOffset = _nOffset;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_jitter"></param>
        internal Jitter(Jitter _jitter)
        {
            this.m_nOffset = _jitter.m_nOffset;
        }

        /// <summary>
        /// Gets or sets offset
        /// </summary>
        public new int Offset
        {
            get { return m_nOffset; }
            set { m_nOffset = value; }
        }

        /// <summary>
        /// executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Random random1 = new Random(this.GetHashCode());
            Random random2 = new Random(this.GetHashCode()*this.GetHashCode());

            int nValue1 = 0;
            int nValue2 = 0;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            int y = 0;
            int x = 0;

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    nValue1 = random1.Next(x, x + m_nOffset);
                    nValue2 = random2.Next(x, x + m_nOffset);
                    if (nValue1 > nWidth - 1)
                    {
                        nValue1 = nWidth - 1;
                    }
                    if (nValue2 > nWidth - 1)
                    {
                        nValue2 = nWidth - 1;
                    }
                    color1 = bitmap.GetPixel(nValue1, y);
                    color2 = bitmap.GetPixel(nValue2, y);
                    if (nValue1 != nValue2)
                    {
                        bitmap.SetPixel(nValue1, y, color2);
                        bitmap.SetPixel(nValue2, y, color1);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Jitter(this);
        }
    }
}
